package com.igoal.service.dwr;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.igoal.dto.CommentDTO;
import com.igoal.dto.DWRPageDTO;
import com.igoal.dto.FriendDTO;
import com.igoal.dto.GoalDTO;
import com.igoal.dto.UserDTO;
import com.igoal.dto.UserGoalDTO;
import com.igoal.service.CommentService;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.SaveResult;
import com.igoal.service.UserGoalManager;
import com.igoal.service.UserManagerExt;

@Service("goalManagerDWR")
public class GoalManagerDWRImpl extends BaseDWRService implements
		GoalManagerDWR {
	@Autowired
	private GoalManager goalManager;

	@Autowired
	private UserManagerExt userManagerExt;
	
	@Autowired
	private UserGoalManager userGoalManager;
	
	@Autowired
	private GoalEntryManager goalEntryManager;
	
	@Autowired
	private CommentService commentService;

	public UserGoalDTO createNewGoal(GoalDTO goal) {
		if (!isLoginUser())
			return null;

		try {
			String userInSession = getUserInSession();
			UserDTO userSession = userManagerExt
					.getUserByUsername(userInSession);

			return userGoalManager.createNewGoal(goal, userSession);
//			return goalManager.createNewGoal(goal, userSession);
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	public int deleteComment(Long commentId){
		if (!isLoginUser())
			return SaveResult.FAIL;
		
		String whoDelete = getUserInSession();
		return commentService.deleteComment(commentId, whoDelete);
	}

	public int removeGoal(Long goalId) {
		if (!isLoginUser())
			return SaveResult.FAIL;

		String userInSession = getUserInSession();
		return userGoalManager.deleteUserGoal(userInSession, goalId);
//		return goalManager.deleteUserGoal(userInSession, goalId);
	}

	public int deleteGoalEntry(Long entryId) {
		if (!isLoginUser())
			return SaveResult.FAIL;

		String userInSession = getUserInSession();
		return goalEntryManager.deleteGoalEntry(userInSession, entryId);
	}

	public int doneGoal(Long goalId) {
		return userGoalManager.achieveGoal(getUserInSession(), goalId);
//		return goalManager.achieveGoal(getUserInSession(), goalId);
	}

	public int doGoal(Long goalId) {
		return userGoalManager.doGoal(getUserInSession(), goalId);
//		return goalManager.doGoal(getUserInSession(), goalId);
	}
	
	public List<String> findGoalByTitle(String title) {
		List<String> result = new ArrayList<String>();
		if(isLoginUser()){
			List<GoalDTO> goals = goalManager.findGoalByTitle(title);
			for(GoalDTO goal : goals){
				String goalTitle = goal.getTitle();
				result.add(goalTitle);
			}
		}
		
		return result;
	}

	public int updateUserGoalPercent(Long goalId, Integer percent) {
		if (!isLoginUser())
			return SaveResult.FAIL;
				
		String userInSession = getUserInSession();
		return userGoalManager.updateUserGoalPercent(userInSession, goalId, percent);
//		return goalManager.updateUserGoalPercent(userInSession, goalId, percent);
	}

	public boolean isThisGoalInUserGoalList(GoalDTO goal) {
		if(!isLoginUser())
			return false;
		
		String userInSession = getUserInSession();
		return userGoalManager.isThisGoalInUserGoalList(userInSession, goal.getTitle());
//		return goalManager.isThisGoalInUserGoalList(userInSession, goal.getTitle());		
	}

	public List<FriendDTO> getRandomEntryLikeList(Long userId, Long entryId, boolean done) {
		return goalEntryManager.getRandomEntryLikeList(userId, entryId, done);
	}

	public DWRPageDTO<FriendDTO> getEntryEntryLikeList(Long entryId, boolean isEntryDone,
			int pageNumber, int totalPage) {
		List<FriendDTO> likeList = goalEntryManager.getEntryLikePage(entryId, isEntryDone, pageNumber);
		DWRPageDTO<FriendDTO> page = new DWRPageDTO<FriendDTO>();
		page.setPageNumber(pageNumber);
		page.setTotalPage(totalPage);
		page.setItems(likeList);

		return page;
	}

	public CommentDTO addComment(CommentDTO dto) {
		if (!isLoginUser())
			return null;
		try {
			String username = getUserInSession();
			Long id = commentService.addComment(dto, username);
			if(id != null && id > 0L){
				dto = commentService.loadComment(id);
				return dto;
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return null;
	}
	
	public int updateTaskStatus(Long taskId, boolean success){
		if (!isLoginUser()){
			return SaveResult.FAIL;			
		}
		
		try{
			String username = getUserInSession();
			if(success){
				return userGoalManager.finishTask(username, taskId);
			}else{
				return userGoalManager.uncompleteTask(username, taskId);
			}
		}catch (Exception e) {
			logger.error(e);
		}
		
		return SaveResult.FAIL;
	}
	
	public int updateTaskSuccess(Long taskId){
		return updateTaskStatus(taskId, true);
	}
	
	public int updateTaskNotSuccess(Long taskId){
		return updateTaskStatus(taskId, false);
	}
}
